package com.sj.parksdk.fragment.profile;

import android.annotation.SuppressLint;
import android.view.View;

import com.sj.parksdk.R;
import com.sj.parksdk.R2;
import com.sj.parksdk.core.BaseFragment;
import com.sj.parksdk.core.http.subscriber.TipRequestSubscriber;
import com.sj.parksdk.net.base.CommonResult;
import com.sj.parksdk.net.http.ApiHelper;
import com.sj.parksdk.setting.Extras;
import com.sj.parksdk.widget.sweetalert.SweetAlertDialog;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.request.CustomRequest;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xrouter.annotation.AutoWired;
import com.xuexiang.xrouter.launcher.XRouter;
import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.utils.ResUtils;
import com.xuexiang.xui.widget.edittext.materialedittext.MaterialEditText;
import com.xuexiang.xui.widget.textview.supertextview.SuperButton;
import com.xuexiang.xutil.common.StringUtils;

import butterknife.BindView;

@Page(name = "设置/修改登录密码", params = {Extras.BASE_CODE})
public class FragmentUpdatePassword extends BaseFragment {
    @BindView(R2.id.et_password)
    MaterialEditText etPassword;
    @BindView(R2.id.btn_sure)
    SuperButton btnSure;

    @AutoWired(name = Extras.BASE_CODE)
    String token;

    @Override
    protected void initArgs() {
        XRouter.getInstance().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.parksdk_fragment_update_password;
    }

    @Override
    protected void initViews() {
        if (StringUtils.isEmpty(token)) {
            btnSure.setEnabled(false);
        }
        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (etPassword.validate()) {
                    if (etPassword.getEditValue().length() > 5 && etPassword.getEditValue().length() < 17)
                        updatePassword();
                    else etPassword.setError(ResUtils.getString(R.string.tip_new_password_error));
                }
            }
        });
    }

    @SuppressLint("CheckResult")
    private void updatePassword() {
        CustomRequest request = XHttp.custom();
        request.call(ApiHelper.getInstance(request)
                .resetPassword(etPassword.getEditValue(), token))
                .subscribeWith(new TipRequestSubscriber<CommonResult>() {
                    @Override
                    protected void onSuccess(CommonResult result) {
                        if (result.getCode() == 200) showSuccessDialog();
                        else showErrorDialog(result.getMessage());
                    }
                });
    }

    private void showSuccessDialog() {
        new SweetAlertDialog(getContext(), SweetAlertDialog.SUCCESS_TYPE)
                .setContentMinHeight(DensityUtils.dp2px(200))
                .setTitleGone(true)
                .setContentText("操作成功")
                .showCancelButton(false)
                .setConfirmText(ResUtils.getString(R.string.back))
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sDialog) {
                        popToBack();
                    }
                }).show();
    }

    private void showErrorDialog(String message) {
        new SweetAlertDialog(getContext(), SweetAlertDialog.ERROR_TYPE)
                .setContentMinHeight(DensityUtils.dp2px(200))
                .setTitleGone(true)
                .setContentText("操作失败,请重新尝试" + (StringUtils.isEmpty(message) ? "" : "\n" + message))
                .showCancelButton(false)
                .setConfirmText(ResUtils.getString(R.string.back))
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sDialog) {
                        popToBack();
                    }
                }).show();
    }
}
